/*
 * mycamera.cpp
 *
 *  Created on: Mar 11, 2013
 *      Author: RDU
 */

#include "mycamera.h"
#include <string>

using namespace std;

MyCamera::MyCamera()
{
	m_Cam1_isOpen=false;
	m_Cam2_isOpen=false;
	m_Cam3_isOpen=false;

	m_showCorners=false;

	m_numImageCam1=0;
	m_numImageCam2=0;
	m_numImageCam3=0;
	m_numImagePair1=0;
	m_numImagePair2=0;

	m_pFrame=NULL;
}

MyCamera::~MyCamera()
{
	m_pFrame=NULL;

	m_Capture1.release();
	m_Capture2.release();
	m_Capture3.release();
}

bool MyCamera::OpenCam1()
{
	m_Cam1_isOpen = m_Capture1.open(0);

	return m_Cam1_isOpen;
}

bool MyCamera::OpenCam2()
{
	m_Cam2_isOpen = m_Capture2.open(1);

	return m_Cam2_isOpen;
}

bool MyCamera::OpenCam3()
{
	m_Cam3_isOpen = m_Capture3.open(2);

	return m_Cam3_isOpen;
}

void MyCamera::CloseCam1()
{
	m_Capture1.release();
	m_Cam1_isOpen=false;
}

void MyCamera::CloseCam2()
{
	m_Capture2.release();
	m_Cam2_isOpen=false;
}

void MyCamera::CloseCam3()
{
	m_Capture3.release();
	m_Cam3_isOpen=false;
}

/**
 * String identity: Cam1/Cam2/Cam3/Pair1/Pair2
 * int num: m_numImageCam1/m_numImageCam2/m_numImageCam3/m_numImagePair2/m_numImagePair2
 */
String MyCamera::GenSavePath(String identity, int num, bool isPair, String pairID)
{
	String path;

	String sNum = static_cast<ostringstream*>( &(ostringstream() << num) )->str();

	if(!isPair)
		path="./Image"+identity+"/"+identity+"_"+sNum+".jpg";
	else
		path="./Image"+identity+"/"+identity+"_"+sNum+"_"+pairID+".jpg";

	return path;
}

bool MyCamera::CaptureCam1()
{
	bool result;
	String imageName=GenSavePath("Cam1",m_numImageCam1,false,"");

	result=imwrite(imageName,m_CapturedImage1);
	if(result)
		m_numImageCam1++;

	return result;
}

bool MyCamera::CaptureCam2()
{
	bool result;
	String imageName=GenSavePath("Cam2",m_numImageCam2,false,"");

	result=imwrite(imageName,m_CapturedImage2);
	if(result)
		m_numImageCam2++;

	return result;
}

bool MyCamera::CaptureCam3()
{
	bool result;
	String imageName=GenSavePath("Cam3",m_numImageCam3,false,"");

	result=imwrite(imageName,m_CapturedImage3);
	if(result)
		m_numImageCam3++;

	return result;
}

bool MyCamera::CapturePair1()
{
	bool result1,result2,final;
	String imageName1=GenSavePath("Pair1",m_numImagePair1,true,"1");
	String imageName2=GenSavePath("Pair1",m_numImagePair1,true,"2");

	result1=imwrite(imageName1,m_CapturedImage1);
	result2=imwrite(imageName2,m_CapturedImage2);
	final=result1 && result2;

	if(final)
		m_numImagePair1++;

	return final;
}

bool MyCamera::CapturePair2()
{
	bool result1,result2,final;
	String imageName1=GenSavePath("Pair2",m_numImagePair2,true,"1");
	String imageName2=GenSavePath("Pair2",m_numImagePair2,true,"2");

	result1=imwrite(imageName1,m_CapturedImage2);
	result2=imwrite(imageName2,m_CapturedImage3);
	final=result1 && result2;

	if(final)
		m_numImagePair2++;

	return final;
}

void MyCamera::CamerasRun()
{
	Size boardSize;

	boardSize.width=8;
	boardSize.height=6;

	if(m_Cam1_isOpen)
		m_Capture1.grab();
	if(m_Cam2_isOpen)
		m_Capture2.grab();
	if(m_Cam3_isOpen)
		m_Capture3.grab();

	if(m_Cam1_isOpen)
	{
		m_Capture1.retrieve(m_CapturedImage1,0);

		if(m_showCorners)
		{
			bool found;
			vector<Point2f> corners;
			Mat capImageCorners;

			m_CapturedImage1.copyTo(capImageCorners);

			found = findChessboardCorners(capImageCorners, boardSize, corners,
						CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);

			drawChessboardCorners(capImageCorners, boardSize, corners, found);

			m_pFrame->GetImagePanel1()->DrawPano(capImageCorners);
		}
		else
			m_pFrame->GetImagePanel1()->DrawPano(m_CapturedImage1);
	}

	if(m_Cam2_isOpen)
	{
		m_Capture2.retrieve(m_CapturedImage2,0);

		if(m_showCorners)
		{
			bool found;
			vector<Point2f> corners;
			Mat capImageCorners;

			m_CapturedImage2.copyTo(capImageCorners);

			found = findChessboardCorners(capImageCorners, boardSize, corners,
						CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);

			drawChessboardCorners(capImageCorners, boardSize, corners, found);

			m_pFrame->GetImagePanel2()->DrawPano(capImageCorners);
		}
		else
			m_pFrame->GetImagePanel2()->DrawPano(m_CapturedImage2);
	}

	if(m_Cam3_isOpen)
	{
		m_Capture3.retrieve(m_CapturedImage3,0);

		if(m_showCorners)
		{
			bool found;
			vector<Point2f> corners;
			Mat capImageCorners;

			m_CapturedImage3.copyTo(capImageCorners);

			found = findChessboardCorners(capImageCorners, boardSize, corners,
						CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);

			drawChessboardCorners(capImageCorners, boardSize, corners, found);

			m_pFrame->GetImagePanel3()->DrawPano(capImageCorners);
		}
		else
			m_pFrame->GetImagePanel3()->DrawPano(m_CapturedImage3);
	}
}

